/*
 * Created by JFormDesigner on Mon Jul 01 14:46:03 CST 2024
 */

package scct.zhanghongbo.ui;

import scct.zhanghongbo.dao.StaffDao;
import scct.zhanghongbo.model.Staff;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 84160
 */
public class DlgStaffModify extends JDialog {
    private final Staff staff;
    private final Integer id;
    public DlgStaffModify(Staff staff,Window owner,Integer id) {
        super(owner);
        this.staff=staff;
        this.id=id;
        initComponents();
        initView();
    }

    private void initView() {
        StaffDao staffDao=new StaffDao();
        Staff staffById = staffDao.getStaffById(id);
        txtStaffName.setText(staffById.getName());
        txtStaffPhone.setText(staffById.getPhone());
        txtStaffAccount.setText(staffById.getAccount());
        txtStaffPass.setText(staffById.getPassword());
        if (staffById.getGender().equals("男")) {
            raMale.setSelected(true);
        } else {
            raFemale.setSelected(false);
        }
        spRank.setValue(staffById.getStaffRank());
        spEhId.setValue(staffById.getEhId());
    }

    private void btnModify(ActionEvent e) {
        // TODO add your code here
        String staffNameText = txtStaffName.getText();
        String gender = raMale.isSelected()?"男":"女";
        String staffAccountText = txtStaffAccount.getText();
        String staffPassText = txtStaffPass.getText();
        String staffPhoneText = txtStaffPhone.getText();
        Integer rankValue = (Integer) spRank.getValue();
        Integer ehId = (Integer) spEhId.getValue();
        StaffDao staffDao=new StaffDao();
        if (staffDao.isExistence(staffAccountText,staffPhoneText,id)) {
            JOptionPane.showMessageDialog(this,"账号或手机号已存在");
            return;
        }
        if (staffNameText.length()>10||staffNameText.length()<3){
            JOptionPane.showMessageDialog(this,"用户名长度不合法（3-10个字符）");
            return;
        }
        if (!staffPassText.matches("^\\w{5,20}$")){
            JOptionPane.showMessageDialog(this,"密码不符合要求（5-20个大小写字母和数字）");
            return;
        }
        if (!staffPhoneText.matches("^1[3-9]\\d{9}$")){
            JOptionPane.showMessageDialog(this,"手机号不合法");
            return;
        }
        if (rankValue>=staff.getStaffRank()){
            JOptionPane.showMessageDialog(this,"员工权限不得高于你的权限");
            return;
        }
        if (staffDao.insert(staffNameText,gender,staffPhoneText,staffAccountText,staffPassText,rankValue,ehId)){
            JOptionPane.showMessageDialog(this,"成功！");
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(this,"失败,也许你可以检查一下是否存在此展馆id");
        }
        StaffRegPanel.getTable();
    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        txtStaffName = new JTextField();
        txtStaffAccount = new JTextField();
        txtStaffPass = new JTextField();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        txtStaffPhone = new JTextField();
        raMale = new JRadioButton();
        raFemale = new JRadioButton();
        spRank = new JSpinner();
        spEhId = new JSpinner();
        btnModify = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u5458\u5de5");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        var contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(txtStaffName);
        txtStaffName.setBounds(181, 35, 145, 34);
        contentPane.add(txtStaffAccount);
        txtStaffAccount.setBounds(181, 85, 145, 34);
        contentPane.add(txtStaffPass);
        txtStaffPass.setBounds(181, 140, 145, 34);

        //---- label1 ----
        label1.setText("\u5458\u5de5\u540d");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label1);
        label1.setBounds(66, 45, 71, 17);

        //---- label2 ----
        label2.setText("\u8d26\u53f7");
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label2);
        label2.setBounds(66, 95, 71, 17);

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label3);
        label3.setBounds(66, 150, 71, 17);

        //---- label4 ----
        label4.setText("\u624b\u673a\u53f7");
        label4.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label4);
        label4.setBounds(66, 200, 71, 17);

        //---- label5 ----
        label5.setText("\u5458\u5de5\u7b49\u7ea7");
        label5.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label5);
        label5.setBounds(66, 250, 71, 17);

        //---- label6 ----
        label6.setText("\u5c55\u9986\u7f16\u53f7");
        label6.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label6);
        label6.setBounds(66, 305, 71, 17);

        //---- label7 ----
        label7.setText("\u6027\u522b");
        label7.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label7);
        label7.setBounds(66, 350, 71, 17);
        contentPane.add(txtStaffPhone);
        txtStaffPhone.setBounds(181, 190, 145, 34);

        //---- raMale ----
        raMale.setText("\u7537");
        raMale.setSelected(true);
        contentPane.add(raMale);
        raMale.setBounds(186, 345, 40, 25);

        //---- raFemale ----
        raFemale.setText("\u5973");
        contentPane.add(raFemale);
        raFemale.setBounds(271, 345, 40, 25);

        //---- spRank ----
        spRank.setModel(new SpinnerNumberModel(0, 0, null, 1));
        contentPane.add(spRank);
        spRank.setBounds(181, 245, 147, 34);

        //---- spEhId ----
        spEhId.setModel(new SpinnerNumberModel(1, 1, null, 1));
        contentPane.add(spEhId);
        spEhId.setBounds(181, 295, 147, 34);

        //---- btnModify ----
        btnModify.setText("\u65b0\u589e");
        btnModify.addActionListener(e -> btnModify(e));
        contentPane.add(btnModify);
        btnModify.setBounds(96, 405, 78, 34);

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        btnCancel.addActionListener(e -> btnCancel(e));
        contentPane.add(btnCancel);
        btnCancel.setBounds(231, 405, 78, 34);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 500);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(raMale);
        buttonGroup1.add(raFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField txtStaffName;
    private JTextField txtStaffAccount;
    private JTextField txtStaffPass;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JTextField txtStaffPhone;
    private JRadioButton raMale;
    private JRadioButton raFemale;
    private JSpinner spRank;
    private JSpinner spEhId;
    private JButton btnModify;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
